﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace OrganizedConfusion
{
    public class Intro : GameState
    {
        int MenuBG, box, img720;
        int xPos, yPos, Width, Height;
        float Alpha;
        bool Up;

        public Intro()
        {
            Up = false;
            xPos = yPos = 0;
            Width = 640;
            Height = 480;
            Alpha = 0.0f;
        }

        public override void LoadContent()
        {
            base.LoadContent();

            box = ContentsManager.Instance.LoadTexture("Textures/box");
            img720 = ContentsManager.Instance.LoadTexture("Textures/1280x720");
            MenuBG = ContentsManager.Instance.LoadTexture("Textures/MenuItem");
            
            Inputmanager.AcquireKeyboard(Keyboard.GetState());
            Inputmanager.AcquireGamePad(GamePad.GetState(PlayerIndex.One));
        }

        public override void Input()
        {
            if (Inputmanager.KeyPressedBuffered(Keys.NumPad1) || Inputmanager.ButtonPressedBuffered(Buttons.Start))
            {
                ContentsManager.Instance.ShutdownContentsManager();
                Statemanager.Changestate(new LoadState("Levels/level1.bin")/*GamePlay()*/);
            }

            if (Inputmanager.KeyPressedBuffered(Keys.O))
                Statemanager.RemoveState(this);

            if (Inputmanager.ButtonPressedBuffered(Buttons.X) || Inputmanager.KeyPressedBuffered(Keys.NumPad3))
                Statemanager.EndGame();

            if (Inputmanager.LeftStickPressure(0).X > 0.5f)
                xPos++;
            if (Inputmanager.LeftStickPressure(0).X < -0.5f)
                xPos--;
            if (Inputmanager.LeftStickPressure(0).Y > 0.5f)
                yPos--;
            if (Inputmanager.LeftStickPressure(0).Y < -0.5f)
                yPos++;

            if (Inputmanager.RightStickPressure(0).X > 0.5f)
                Width++;
            if (Inputmanager.RightStickPressure(0).X < -0.5f)
                Width--;
            if (Inputmanager.RightStickPressure(0).Y > 0.5f)
                Height--;
            if (Inputmanager.RightStickPressure(0).Y < -0.5f)
                Height++;
        }

        public override void Update(GameTime gameTime)
        {
            Input();
            if (Up)
            {
                Alpha += 0.005f;
                if (Alpha > 1)
                    Up = false;
            }
            else
            {
                Alpha -= 0.005f;
                if (Alpha < 0)
                    Up = true;
            }
        }

        public override void Render(GameTime gameTime)
        {
            SpriteBatch spriteBatch = Statemanager.Spritebatch;
            //Viewport viewport = Statemanager.GraphicsDevice.Viewport;

            Rectangle menu = new Rectangle(500, 360, 300, 80);

            spriteBatch.Begin();
            Statemanager.GraphicsDevice.Clear(Color.Black);

            spriteBatch.Draw(ContentsManager.Instance.Draw(img720), new Vector2(0, 0), Color.Lerp(Color.White, Color.Transparent, Alpha));

            spriteBatch.Draw(ContentsManager.Instance.Draw(MenuBG), new Vector2(500, 360), null, Color.White, 0.0f, new Vector2(30,20), 0.8f, SpriteEffects.None, 0.0f);
            spriteBatch.DrawString(Statemanager.Font, "1) Play Game", new Vector2(500, 360), Color.Violet);

            spriteBatch.Draw(ContentsManager.Instance.Draw(MenuBG), new Vector2(500, 420), null, Color.White, 0.0f, new Vector2(30, 20), 0.8f, SpriteEffects.None, 0.0f);
            spriteBatch.DrawString(Statemanager.Font, "2) Options", new Vector2(500, 420), Color.Violet);

            spriteBatch.Draw(ContentsManager.Instance.Draw(MenuBG), new Vector2(500, 480), null, Color.White, 0.0f, new Vector2(30, 20), 0.8f, SpriteEffects.None, 0.0f);
            spriteBatch.DrawString(Statemanager.Font, "3) Exit", new Vector2(500, 480), Color.Violet);

            spriteBatch.End();
        }
    }
}
